package com.zxc.controller.data;

import com.zxc.dto.DictManager;
import com.zxc.entity.CnRegionInfo;
import com.zxc.entity.CstLevel;
import com.zxc.entity.CstType;
import com.zxc.service.CstLevelService;
import com.zxc.service.CstRegionService;
import com.zxc.service.CstTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/dict")
public class DictController {
    //客户等级
    @Autowired
    private CstLevelService cstLevelService;

    //服务类型
    @Autowired
    private CstTypeService cstTypeService;

    //地区
    @Autowired
    private CstRegionService cstRegionService;

    @RequestMapping("/{skipCount}/{pageSize}")
    @ResponseBody
    public Map<String,Object> getStorageListOrByCustName(@PathVariable Integer skipCount,
                                                         @PathVariable Integer pageSize){
        Map<String,Object> modelMap = new HashMap<>();
        //dataList,total
        List<DictManager> dataList = new ArrayList<>();

        //我们只对地区表进行分页
        int pageNo = (skipCount-1)*pageSize;

        //企业客户等级
        List<CstLevel> cstLevelList =  cstLevelService.selectAll();
        for (CstLevel cstLevel : cstLevelList){
            DictManager dictManager = new DictManager();
            dictManager.setType("企业用户等级");
            dictManager.setIdEdit(false);
            dictManager.setItem(cstLevel.getCstLevelName());
            dictManager.setValue(cstLevel.getCstLevelNo());
            dataList.add(dictManager);
        }

        //服务类型
        List<CstType> cstTypeList = cstTypeService.getCstTypeService();
        for (CstType cstType : cstTypeList){
            DictManager dictManager = new DictManager();
            dictManager.setIdEdit(true);
            dictManager.setType("服务类型");
            dictManager.setItem(cstType.getTypeName());
            dictManager.setValue(cstType.getTypeName());
            dataList.add(dictManager);
        }

        //地区
        List<CnRegionInfo> cnRegionInfoList = cstRegionService.selectAll(pageNo,pageSize);
        for (CnRegionInfo cnRegionInfo : cnRegionInfoList){
            DictManager dictManager = new DictManager();
            dictManager.setIdEdit(true);
            dictManager.setType("地区");
            dictManager.setItem(cnRegionInfo.getCriShortName());
            dictManager.setValue(cnRegionInfo.getCriCode());
            dataList.add(dictManager);
        }

        //地区表的所有数据数
        int regionCount = cstRegionService.selectCount();

        modelMap.put("dataList",dataList);
        modelMap.put("total",regionCount);
        return modelMap;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> deleteByItem(String[] item){
        Map<String,Object> modelMap = new HashMap<>();
        String[] cstTypeName = cstTypeService.getAllCstTypeName();
        //创建一个需要删除的服务类型的集合
        List<String> deleteTypeName = new ArrayList<>();
        //创建一个需要删除的地区类型的集合
        List<String> deleteRegion = new ArrayList<>();
        for (String s : item){
            boolean flag = false;
            for (String typeName : cstTypeName){
                //一旦同前端传送过来的对应上了，就说明是服务类型表里面的数据
                if (s.equals(typeName)){
                    flag = true;
                    //如果直接匹配上了，那么直接跳出此次循环
                    continue;
                }
            }
            //可以确定是服务类型列表的数据，这里我们可以根据服务类型名进行删除操作
            if (flag){
                deleteTypeName.add(s);
            }else {
                //如果不为服务类型列表数据，那么就一定是为地区表数据了
                deleteRegion.add(s);
            }
        }
        //循环结束需要删除的服务类型集合
        int typeNameResult = 0;
        int regionResult = 0;

        if (deleteTypeName.size() > 0){
            typeNameResult = cstTypeService.deleteByTypeName(deleteTypeName);
            if (typeNameResult > 0){
                modelMap.put("success",true);
            }else {
                modelMap.put("success",false);
            }
        }

        if (deleteRegion.size() > 0){
            regionResult = cstRegionService.deleteByCriCode(deleteRegion);
            if (regionResult > 0){
                modelMap.put("success",true);
            }else {
                modelMap.put("success",false);
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String,Object> save(DictManager dictManager){
        Map<String,Object> modelMap = new HashMap<>();
        System.out.println("前端接收到的条目=="+dictManager.toString());

        //有三种可能
        if (dictManager.getType().equals("企业用户等级")){
            //那么就进入cst_level表
            CstLevel cstLevel = new CstLevel();
            cstLevel.setCstLevelName(dictManager.getItem());
            cstLevel.setCstLevelNo(dictManager.getValue());
            int result = cstLevelService.saveCstLevel(cstLevel);
            if (result > 0){
                modelMap.put("success",true);
            }else {
                modelMap.put("success",false);
            }
        }else if (dictManager.getType().equals("服务类型")){
            //进入cst_type表
            CstType cstType = new CstType();
            cstType.setTypeName(dictManager.getItem());
            int result = cstTypeService.saveCstType(cstType);
            if (result > 0){
                modelMap.put("success",true);
            }else {
                modelMap.put("success",false);
            }
        }else {
            //进入cn_region_info表
            CnRegionInfo cnRegionInfo = new CnRegionInfo();
            cnRegionInfo.setCriCode(dictManager.getValue());
            cnRegionInfo.setCriName(dictManager.getItem());
            int result = cstRegionService.saveCnRegionInfo(cnRegionInfo);
            if (result > 0){
                modelMap.put("success",true);
            }else {
                modelMap.put("success",false);
            }
        }
        return modelMap;
    }


    @RequestMapping(value = "/initDictManager/{searchName}")
    @ResponseBody
    public Map<String,Object> updateDictManager(@PathVariable String searchName){
        Map<String,Object> modelMap = new HashMap<>();
        String[] typeName = cstTypeService.getAllCstTypeName();
        boolean flag = false;
        for (String s : typeName){
            if (s.equals(searchName)){
                //如果进入该if那个就是属于服务类型
                CstType cstType = cstTypeService.getCstTypeByTypeName(searchName);
                DictManager dictManager = new DictManager();
                dictManager.setItem(cstType.getTypeName());
                dictManager.setType("服务类型");
                dictManager.setValue(cstType.getTypeName());
                modelMap.put("dictManager",dictManager);
                dictManager.setIdEdit(true);
                flag = true;
            }
        }

        //如果flag为false,说明需要初始化的不是服务类型是地区类型的数据
        if (!flag){
            List<CnRegionInfo> cnRegionInfoList  =cstRegionService.getCnRegionInfoByCriShortName(searchName);
            CnRegionInfo cnRegionInfo = cnRegionInfoList.get(0);
            DictManager dictManager = new DictManager();
            dictManager.setType("地区");
            dictManager.setValue(cnRegionInfo.getCriCode());
            dictManager.setItem(cnRegionInfo.getCriShortName());
            dictManager.setIdEdit(true);
            modelMap.put("dictManager",dictManager);
        }
        return modelMap;
    }



}
